package com.soc.web.client.api;

import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.json.client.JSONObject;
import com.soc.web.client.api.events.CancelHandler;
import com.soc.web.client.api.events.CleanupHandler;
import com.soc.web.client.api.events.ClosedHandler;
import com.soc.web.client.api.events.CompleteHandler;
import com.soc.web.client.api.events.StartHandler;
import com.soc.web.client.api.events.TitleFormatHandler;

public class BaseOptions {
	protected JSONObject optionsObject;
	public BaseOptions() {
		this.optionsObject = new JSONObject();
	}
	
	/**
	 * Will be called once the content is displayed
	 * @param callback the event callback
	 */
	public native void onComplete(CompleteHandler callback) /*-{
		var opts = this.@com.soc.web.client.api.BaseOptions::optionsObject;
		var that = callback;
		// Create the function object
		var optObject = function() {
			that.@com.soc.web.client.api.events.CompleteHandler::onComplete()();	
		}	
		// Convert to the expected JSONValue
		var jsObject = @com.soc.web.client.api.FancyboxUtils::convertJSO(Lcom/google/gwt/core/client/JavaScriptObject;)(optObject);
		opts.@com.google.gwt.json.client.JSONObject::put(Ljava/lang/String;Lcom/google/gwt/json/client/JSONValue;)("onComplete", jsObject);

	}-*/;
	
	/**
	 * Will be called once FancyBox is closed
	 * @param callback the event callback
	 */
	public native void onClosed(ClosedHandler callback) /*-{
		var opts = this.@com.soc.web.client.api.BaseOptions::optionsObject;
		var that = callback;
		// Create the function object
		var optObject = function() {
			that.@com.soc.web.client.api.events.ClosedHandler::onClosed()();	
		}	
		// Convert to the expected JSONValue
		var jsObject = @com.soc.web.client.api.FancyboxUtils::convertJSO(Lcom/google/gwt/core/client/JavaScriptObject;)(optObject);
		opts.@com.google.gwt.json.client.JSONObject::put(Ljava/lang/String;Lcom/google/gwt/json/client/JSONValue;)("onClosed", jsObject);
	}-*/;
	
	/**
	 * Will be called just before closing
	 * @param callback the event callback
	 */
	public native void onCleanup(CleanupHandler callback) /*-{
		var opts = this.@com.soc.web.client.api.BaseOptions::optionsObject;
		var that = callback;
		// Create the function object
		var optObject = function() {
			that.@com.soc.web.client.api.events.CleanupHandler::onCleanup()();
		}	
		// Convert to the expected JSONValue
		var jsObject = @com.soc.web.client.api.FancyboxUtils::convertJSO(Lcom/google/gwt/core/client/JavaScriptObject;)(optObject);
		opts.@com.google.gwt.json.client.JSONObject::put(Ljava/lang/String;Lcom/google/gwt/json/client/JSONValue;)("onCleanup", jsObject);
	}-*/;
	
	/**
	 * Will be called right before attempting to load the content
	 * @param callback the event callback
	 */
	public native void onStart(StartHandler callback) /*-{
		var opts = this.@com.soc.web.client.api.BaseOptions::optionsObject;
		var that = callback;
		// Create the function object
		var optObject = function() {
			that.@com.soc.web.client.api.events.StartHandler::onStart()();
		}	
		// Convert to the expected JSONValue
		var jsObject = @com.soc.web.client.api.FancyboxUtils::convertJSO(Lcom/google/gwt/core/client/JavaScriptObject;)(optObject);
		opts.@com.google.gwt.json.client.JSONObject::put(Ljava/lang/String;Lcom/google/gwt/json/client/JSONValue;)("onStart", jsObject);
	}-*/;
	
	/**
	 * Will be called after loading is canceled
	 * @param callback the event callback
	 */
	public native void onCancel(CancelHandler callback) /*-{
		var opts = this.@com.soc.web.client.api.BaseOptions::optionsObject;
		var that = callback;
		// Create the function object
		var optObject = function() {
			that.@com.soc.web.client.api.events.CancelHandler::onCancel()();
		}	
		// Convert to the expected JSONValue
		var jsObject = @com.soc.web.client.api.FancyboxUtils::convertJSO(Lcom/google/gwt/core/client/JavaScriptObject;)(optObject);
		opts.@com.google.gwt.json.client.JSONObject::put(Ljava/lang/String;Lcom/google/gwt/json/client/JSONValue;)("onCancel", jsObject);
	}-*/;

	/**
	 * Callback to customize title area. You can set any html - custom image counter or even custom navigation
	 * @param callback
	 */
	public void addTitleFormat(TitleFormatHandler callback) {
		optionsObject.put("titleFormat", new JSONObject(createTitleFormatCallback(callback)));
	}
	
	private native JavaScriptObject createTitleFormatCallback(TitleFormatHandler callback) /*-{
		var that = callback;
		var optObject = function(title, currentArray, currentIndex, currentOpts) {
			return that.@com.soc.web.client.api.events.TitleFormatHandler::call(Ljava/lang/String;Lcom/google/gwt/core/client/JavaScriptObject;ILcom/google/gwt/core/client/JavaScriptObject;)(title, currentArray, currentIndex, currentOpts);
		}		
		return optObject;	
	}-*/;	
	
	
}
